//package com.wejoy.video.middle.station.api.controller.smart;
//
//import com.wejoy.video.middle.station.data.constant.RedisKeys;
//import com.wejoy.video.middle.station.data.domain.PageResult;
//import com.wejoy.video.middle.station.data.domain.system.SysCarmera;
//import com.wejoy.video.middle.station.data.domain.system.SysDept;
//import com.wejoy.video.middle.station.data.domain.workorder.*;
//import com.wejoy.video.middle.station.data.enums.dict.EnumDeptType;
//import com.wejoy.video.middle.station.data.enums.dict.EnumWorkOrderStatus;
//import com.wejoy.video.middle.station.data.exception.BusinessException;
//import com.wejoy.video.middle.station.data.service.evs.IEvsHttpService;
//import com.wejoy.video.middle.station.data.service.system.ISysCarmeraService;
//import com.wejoy.video.middle.station.data.service.system.ISysDeptService;
//import com.wejoy.video.middle.station.data.service.system.ISysDeptVideoService;
//import com.wejoy.video.middle.station.data.service.workorder.*;
//import com.wejoy.video.middle.station.data.util.ConstantValues;
//import com.wejoy.video.middle.station.data.util.Md5Utils;
//import com.wejoy.video.middle.station.data.util.StringUtils;
//import com.wejoy.video.middle.station.data.util.WJAssert;
//import com.wejoy.video.middle.station.data.vo.RespJSON;
//import com.wejoy.video.middle.station.data.vo.api.AlarmsNumsVO;
//import com.wejoy.video.middle.station.data.vo.api.BusinessOverbiewVO;
//import com.wejoy.video.middle.station.data.vo.api.DeptNode;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.util.ObjectUtils;
//import org.springframework.web.bind.annotation.*;
//
//import java.io.IOException;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Optional;
//import java.util.stream.Collectors;
//
///**
// * @Author miracle_j
// * Create by 2024/5/21 9:31 AM
// */
//@RestController
//@RequestMapping("smart/")
//public class SmartAction {
//
//    private static final HashMap<String,String> APPKEY = new HashMap<>();
//    // 静态初始化块
//    static {
//        APPKEY.put("06C5508A921F439B83370E4319B99C91", "5478766406ED4BB5981FB63EA5CCAA19");
//    }
//    @Autowired
//    private IEvsHttpService evsHttpService;
//    @Autowired
//    private ISysDeptVideoService sysDeptVideoService;
//    @Autowired
//    private ISysDeptService deptService;
//    @Autowired
//    private RedisTemplate redisTemplate;
//    @Autowired
//    private IWorkOrderInfoService workOrderInfoService;
//    @Autowired
//    private IWorkOrderProcessService workOrderProcessService;
//    @Autowired
//    private IWorkOrderReviewService workOrderReviewService;
//    @Autowired
//    private IOrderPhotoLibService orderPhotoLibService;
//    @Autowired
//    private ISysDeptService sysDeptService;
//    @Autowired
//    private ISysCarmeraService sysCarmeraService;
//    @Autowired
//    private IOrderAlarmAssortService orderAlarmAssortService;
//    @Autowired
//    private IOrderAlarmInfoService orderAlarmInfoService;
//    /**
//     * 获取部门树
//     * @return
//     * @throws IOException
//     */
//    @ResponseBody
//    @GetMapping("/getDeptTree")
//    public RespJSON getDeptTree(
//                                @RequestHeader(value = ConstantValues.API_HEADER_APP_SIGNATURE_KEY,required = false) String signature,
//                                @RequestHeader(value = ConstantValues.API_HEADER_APP_KEY,required = false) String appkey,
//                                @RequestHeader(value = ConstantValues.API_HEADER_APP_TIMESTAMP_KEY,required = false) Long timestamp) throws Exception {
//        checkSign(signature,appkey,timestamp);
//        SysDept evsApiDept = new SysDept();
//        evsApiDept.setParentId(-1);
//        evsApiDept.setIncludeLower(true);
//        List<DeptNode> data = sysDeptService.convertToDesiredFormat(evsHttpService.getDepartmentsJson(evsApiDept, evsHttpService.getToken()));
//        List<SysDept> list = sysDeptService.getList(new SysDept());
//        data.forEach(item->{
//            Optional<SysDept> first = list.stream()
//                    .filter(de -> de.getId()==item.getId())
//                    .findFirst();
//            if(first.isPresent()){
//                SysDept sysDept = first.get();
//                item.setAlarmsCount(sysDept.getAlarmsCount());
//                item.setPoiId(sysDept.getPoiId());
//                item.setPoiName(sysDept.getPoiName());
//                item.setDescribes(sysDept.getDescribes());
//                item.setAiCameraCount(sysDept.getAiCameraCount());
//                item.setCameraCount(sysDept.getCameraCount());
//                item.setUserId(sysDept.getUserId());
//                item.setDeptType(sysDept.getDeptType());
//                item.setLatitude(sysDept.getLatitude());
//                item.setLongitude(sysDept.getLongitude());
//                item.setArea(sysDept.getArea());
//                item.setDispatchId(sysDept.getDispatchId());
//                item.setUeArea(sysDept.getUeArea());
//            }
//        });
//        return RespJSON.success(data);
//    }
//
//    /**
//     * 报警数量查询
//     * @param workOrderInfoReq
//     * @return
//     */
//    @PostMapping ("/numberOfAlarms")
//    public RespJSON numberOfAlarms(@RequestBody(required = false) WorkOrderInfo workOrderInfoReq,
//                                   @RequestHeader(value = ConstantValues.API_HEADER_APP_SIGNATURE_KEY,required = false) String signature,
//                                   @RequestHeader(value = ConstantValues.API_HEADER_APP_KEY,required = false) String appkey,
//                                   @RequestHeader(value = ConstantValues.API_HEADER_APP_TIMESTAMP_KEY,required = false) Long timestamp) throws Exception {
//        checkSign(signature,appkey,timestamp);
//        AlarmsNumsVO vo = new AlarmsNumsVO();
//        workOrderInfoReq.setOrderStatus(EnumWorkOrderStatus.FINISHED.getCode());
//        vo.setFinished(workOrderInfoService.getCount(workOrderInfoReq));
//        workOrderInfoReq.setOrderStatus(null);
//        vo.setTheTotalNumber(workOrderInfoService.getCount(workOrderInfoReq));
//        workOrderInfoReq.setSearchUndone(true);
//        vo.setOrderReceived(workOrderInfoService.getCount(workOrderInfoReq));
//        workOrderInfoReq.setSearchUndone(false);
//        workOrderInfoReq.setOrderStatus(EnumWorkOrderStatus.AGENCY.getCode());
//        vo.setCurrentAlarm(workOrderInfoService.getCount(workOrderInfoReq));
//        return RespJSON.success(vo);
//    }
//
//    /**
//     * 分页查询
//     *
//     * @param workOrderInfoReq workOrderInfoReq
//     * @return RespJSON
//     */
//    @PostMapping  ("/getOrderPageList")
//    public RespJSON getPageList(@RequestBody(required = false) WorkOrderInfo workOrderInfoReq,
//                                @RequestHeader(value = ConstantValues.API_HEADER_APP_SIGNATURE_KEY,required = false) String signature,
//                                @RequestHeader(value = ConstantValues.API_HEADER_APP_KEY,required = false) String appkey,
//                                @RequestHeader(value = ConstantValues.API_HEADER_APP_TIMESTAMP_KEY,required = false) Long timestamp)  throws Exception {
//        checkSign(signature,appkey,timestamp);
//        if(workOrderInfoReq.getOrderStatus()!=null&&workOrderInfoReq.getOrderStatus()==-1){
//            workOrderInfoReq.setSearchUndone(true);
//            workOrderInfoReq.setOrderStatus(null);
//        }
//        int count = workOrderInfoService.getCount(workOrderInfoReq);
//        List<WorkOrderInfo> list = new ArrayList<>();
//        if (count > 0) {
//            list = workOrderInfoService.getPageList(workOrderInfoReq).stream().peek(item ->{
//                WorkOrderProcess processWhere = new WorkOrderProcess();
//                processWhere.setOrderId(item.getId());
//                item.setProcessList(workOrderProcessService.getList(processWhere));
//                WorkOrderReview reviewWhere = new WorkOrderReview();
//                reviewWhere.setOrderId(item.getId());
//                OrderPhotoLib orderPhotoLib = new OrderPhotoLib();
//                orderPhotoLib.setOrderId(item.getId());
//                List<OrderPhotoLib> photoLibs = orderPhotoLibService.getList(orderPhotoLib);
//                item.setPhotoLibs(photoLibs);
//                item.setReviewList(workOrderReviewService.getList(reviewWhere));
//            }).collect(Collectors.toList());
//        }
//        PageResult result = new PageResult(workOrderInfoReq.getPageNo(), workOrderInfoReq.getPageSize(), count, list);
//        return RespJSON.success(result);
//    }
//
//    /**
//     * 业务场景报警统计
//     * @param workOrderInfoReq
//     * @return
//     */
//    @PostMapping ("/sceneStatistics")
//    public RespJSON sceneStatistics(@RequestBody(required = false) WorkOrderInfo workOrderInfoReq,
//                                    @RequestHeader(value = ConstantValues.API_HEADER_APP_SIGNATURE_KEY,required = false) String signature,
//                                    @RequestHeader(value = ConstantValues.API_HEADER_APP_KEY,required = false) String appkey,
//                                    @RequestHeader(value = ConstantValues.API_HEADER_APP_TIMESTAMP_KEY,required = false) Long timestamp)  throws Exception {
//        checkSign(signature,appkey,timestamp);
//        List<OrderAlarmAssort> list = orderAlarmAssortService.getList(new OrderAlarmAssort());
//        List<OrderAlarmInfo> infoServiceList = orderAlarmInfoService.getList(new OrderAlarmInfo());
//        if(ObjectUtils.isEmpty(workOrderInfoReq)){
//            workOrderInfoReq=new WorkOrderInfo();
//        }
//        for (OrderAlarmAssort item : list) {
//            if(item.getLevel()==1){
//                workOrderInfoReq.setMainTypeId(item.getId());
//            }else {
//                workOrderInfoReq.setMinorTypeId(item.getId());
//            }
//            item.setCount(workOrderInfoService.getCount(workOrderInfoReq));
//            workOrderInfoReq.setMinorTypeId(null);
//            workOrderInfoReq.setMainTypeId(null);
//        }
//        for (OrderAlarmInfo orderAlarmInfo : infoServiceList) {
//            workOrderInfoReq.setBusinessScenario(orderAlarmInfo.getCallBackName());
//            int count = workOrderInfoService.getCount(workOrderInfoReq);
//            OrderAlarmAssort orderAlarmAssort = new OrderAlarmAssort();
//            orderAlarmAssort.setCount(count);
//            orderAlarmAssort.setName(orderAlarmInfo.getName());
//            orderAlarmAssort.setParentId(orderAlarmInfo.getMinorTypeId());
//            orderAlarmAssort.setLevel(3);
//            list.add(orderAlarmAssort);
//        }
//        return RespJSON.success(list);
//    }
//
//    @GetMapping("/businessOverview")
//    public RespJSON businessOverview(Long id,
//                                     @RequestHeader(value = ConstantValues.API_HEADER_APP_SIGNATURE_KEY,required = false) String signature,
//                                     @RequestHeader(value = ConstantValues.API_HEADER_APP_KEY,required = false) String appkey,
//                                     @RequestHeader(value = ConstantValues.API_HEADER_APP_TIMESTAMP_KEY,required = false) Long timestamp) throws Exception {
//        checkSign(signature,appkey,timestamp);
//        BusinessOverbiewVO businessOverbiewVO = new BusinessOverbiewVO();
//        //如果区域不为空，则按区域查询
////        if(!StringUtils.isEmpty(sysDept.getArea())){
////            List<SysDept> list = sysDeptService.getList(sysDept);
////            extractedArea(businessOverbiewVO,list,sysDept.getArea());
////            return RespJSON.success(businessOverbiewVO);
////        }
//        List<SysDept> list = sysDeptService.getList(new SysDept());
//        //如果没有参数 查询所有部门业务概况
//        if(ObjectUtils.isEmpty(id)){
//            extracted(businessOverbiewVO, list,false,null);
//            Object officeAiNum = redisTemplate.opsForValue().get(RedisKeys.ALL_AI_NUM);
//            SysCarmera sysDeptCameraEq = new SysCarmera();
//            sysDeptCameraEq.setDepType(EnumDeptType.DEPT_BUSINESS_HALL.getCode());
//            sysDeptCameraEq.setOnline(true);
//            sysDeptCameraEq.setIsAi(true);
//            businessOverbiewVO.setOfficeAiOnLineCameraNum(Long.valueOf(sysCarmeraService.getCount(sysDeptCameraEq)));
//            sysDeptCameraEq.setDepType(EnumDeptType.DEPT_STATIONS.getCode());
//            businessOverbiewVO.setSiteAiOnLineCameraNum(Long.valueOf(sysCarmeraService.getCount(sysDeptCameraEq)));
//            businessOverbiewVO.setAiNum(officeAiNum==null?0L:Long.valueOf(officeAiNum.toString()));
//            Object o = redisTemplate.opsForValue().get(RedisKeys.AI_NUMS);
//            businessOverbiewVO.setAiCameraRoadNum(o==null?0:Integer.valueOf(o.toString()));
//            return RespJSON.success(businessOverbiewVO);
//        }
//        WJAssert.notNull(id);
//        SysDept dept = sysDeptService.getOneById(id);
//        if(ObjectUtils.isEmpty(dept)){
//            return RespJSON.success();
//        }
//        businessOverbiewVO.setSiteArea(dept.getSiteArea());
//        businessOverbiewVO.setModel(dept.getIsModel());
//        businessOverbiewVO.setModelName(dept.getModelName());
//        //如果是调度中心
//        if(dept.getDeptType().equals(EnumDeptType.DEPT_DISPATCH_CENTER.getCode())){
//            extracted(businessOverbiewVO, list,true,dept.getId());
//            Object aiNum = redisTemplate.opsForValue().get(RedisKeys.DISPATCH_AI_NUM+":"+dept.getId());
//            businessOverbiewVO.setAiNum(aiNum==null?0L:Long.valueOf(aiNum.toString()));
//            businessOverbiewVO.setAiNum(businessOverbiewVO.getOfficeAiNum()+ businessOverbiewVO.getSiteAiNum());
//            return RespJSON.success(businessOverbiewVO);
//        }
//        //如果是营业厅
//        if(dept.getDeptType().equals(EnumDeptType.DEPT_BUSINESS_HALL.getCode())){
//            businessOverbiewVO.setOfficeCameraNum(Long.valueOf(dept.getCameraCount()==null?0L:dept.getCameraCount()));
//            businessOverbiewVO.setOfficeAiCameraNum(Long.valueOf(dept.getAiCameraCount()==null?0L:dept.getAiCameraCount()));
//            return getRespJSON(businessOverbiewVO, dept);
//        }
//        //如果是场站
//        if(dept.getDeptType().equals(EnumDeptType.DEPT_STATIONS.getCode())){
//            businessOverbiewVO.setSiteCameraNum(Long.valueOf(dept.getCameraCount()==null?0L:dept.getCameraCount()));
//            businessOverbiewVO.setSiteAiCameraNum(Long.valueOf(dept.getAiCameraCount()==null?0L:dept.getAiCameraCount()));
//            return getRespJSON(businessOverbiewVO, dept);
//        }
//        return RespJSON.success();
//    }
//
//    private RespJSON getRespJSON(BusinessOverbiewVO businessOverbiewVO, SysDept dept) {
//        SysCarmera sysDeptCamera = new SysCarmera();
//        sysDeptCamera.setDepId(dept.getId());
//        List<SysCarmera> deptCameras = sysCarmeraService.getList(sysDeptCamera);
//        Long offlineNum=0L;
//        Long onlineNum=0L;
//        for (SysCarmera deptCamera : deptCameras) {
//            if(deptCamera.getOnline()){
//                onlineNum=onlineNum+1;
//            }else {
//                offlineNum=offlineNum+1;
//            }
//        }
//        businessOverbiewVO.setOfflineCameraNum(offlineNum);
//        businessOverbiewVO.setOnlineCameraNum(onlineNum);
//        return RespJSON.success(businessOverbiewVO);
//    }
//
//    private void extracted(BusinessOverbiewVO businessOverbiewVO, List<SysDept> list, Boolean isDispatch,Long dispatchId) {
//        List<SysDept> sites ;
//        List<SysDept> offices;
//        if(!isDispatch){
//            sites = list.stream().filter(item -> item.getDeptType() != null && item.getDeptType() != null && item.getDeptType().equals(EnumDeptType.DEPT_STATIONS.getCode())).collect(Collectors.toList());
//            offices = list.stream().filter(item -> item.getDeptType() != null && item.getDeptType() != null && item.getDeptType().equals(EnumDeptType.DEPT_BUSINESS_HALL.getCode())).collect(Collectors.toList());
//        }else {
//            sites = list.stream().filter(item -> item.getDeptType() != null && item.getDeptType().equals(EnumDeptType.DEPT_STATIONS.getCode()) && item.getDispatchId() != null && item.getDispatchId().equals(dispatchId)).collect(Collectors.toList());
//            offices = list.stream().filter(item -> item.getDeptType() != null && item.getDeptType().equals(EnumDeptType.DEPT_BUSINESS_HALL.getCode())&& item.getDispatchId() != null && item.getDispatchId().equals(dispatchId)).collect(Collectors.toList());
//        }
//        Long officeCamerNum=0L;
//        Long officeAiCamerNum=0L;
//        Long siteCamerNum=0L;
//        Long siteAiCamerNum=0L;
//        //营业厅数量
//        businessOverbiewVO.setOfficeNum(Long.valueOf(offices.size()));
//        for (SysDept office : offices) {
//            officeCamerNum=officeCamerNum+(office.getCameraCount()==null?0L:office.getCameraCount());
//            officeAiCamerNum=officeAiCamerNum+(office.getAiCameraCount()==null?0L:office.getAiCameraCount());
//        }
//        //营业厅摄像头数量
//        businessOverbiewVO.setOfficeCameraNum(officeCamerNum);
//        //营业厅AI摄像头数量
//        businessOverbiewVO.setOfficeAiCameraNum(officeAiCamerNum);
//        //场站数量
//        businessOverbiewVO.setSiteNum(Long.valueOf(sites.size()));
//        for (SysDept site : sites) {
//            siteCamerNum=siteCamerNum+(site.getCameraCount()==null?0L:site.getCameraCount());
//            siteAiCamerNum=siteAiCamerNum+(site.getAiCameraCount()==null?0L:site.getAiCameraCount());
//        }
//        //场站AI摄像头数量
//        businessOverbiewVO.setSiteAiCameraNum(siteAiCamerNum);
//        //场站摄像头数量
//        businessOverbiewVO.setSiteCameraNum(siteCamerNum);
//        if(isDispatch){
//            Object siteAiNum = redisTemplate.opsForValue().get(RedisKeys.DISPATCH_STATIONS_AI_NUM + ":" + dispatchId);
//            Object officeAiNum = redisTemplate.opsForValue().get(RedisKeys.DISPATCH_OFFICES_AI_NUM + ":" + dispatchId);
//            businessOverbiewVO.setSiteAiNum(siteAiNum==null?0L:Long.valueOf(siteAiNum.toString()));
//            businessOverbiewVO.setOfficeAiNum( officeAiNum==null?0L:Long.valueOf(officeAiNum.toString()));
//        }else {
//            //总数场站AI算法种类
//            Object siteAiNum = redisTemplate.opsForValue().get(RedisKeys.STATIONS_NUM);
//            businessOverbiewVO.setSiteAiNum(siteAiNum==null?0L:Long.valueOf(siteAiNum.toString()));
//            //总数营业厅AI算法种类
//            Object officeAiNum = redisTemplate.opsForValue().get(RedisKeys.OFFICES_NUM);
//            businessOverbiewVO.setOfficeAiNum( officeAiNum==null?0L:Long.valueOf(officeAiNum.toString()));
//        }
//    }
//
//    private void checkSign(String signature,String appkey,Long timestamp) throws Exception {
//        WJAssert.notBlank(signature,"signature不能为空");
//        WJAssert.notBlank(appkey,"appkey不能为空");
//        WJAssert.notNull(timestamp,"timestamp不能为空");
//        String secret = APPKEY.get(appkey);
//        //签名，通过“appkey+secret+timestamp”进行md5加密，得到的32位MD5值。其中加号也参与MD5的计算。
//        String mySignature = Md5Utils.hash(appkey+"+"+secret+"+"+timestamp);
//        if(!signature.equals(mySignature)){
//            throw new BusinessException("签名错误!");
//        }
//        //每次请求实时计算签名，签名有效期为10分钟。
//        Object o = redisTemplate.opsForValue().get(RedisKeys.SMART_API_SIGN_TIME + timestamp);
//        if(!ObjectUtils.isEmpty(o)){
//            throw new BusinessException("签名已过期！");
//        }
//        redisTemplate.opsForValue().set(RedisKeys.SMART_API_SIGN_TIME + timestamp,signature,600);
//    }
//
//
//}
